package com.cheng.controller;

import com.cheng.pojo.User;
import com.cheng.service.user.UserService;
import com.cheng.utils.UUIDutils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@SuppressWarnings("all")
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginPage() {
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public User login(String account, String password) {
        User user = new User();
        user.setUUser(account);
        user.setUPwd(password);
        user = userService.getUser(user);

        if (user == null) {
            user = new User();
        }
        return user;
    }

    @RequestMapping(value = "/listPage")
    public String listPage() {
        return "list";
    }

    @RequestMapping(value = "/getAll")
    @ResponseBody
    public List<User> getAll() {
        List<User> list = userService.getAll();
        if (list == null) {
            list = new ArrayList<>();
        }
        return list;
    }

    @RequestMapping(value = "/updatePage/{id}")
    public ModelAndView updatePage(@PathVariable Integer id, ModelAndView modelAndView) {
        User user = new User();
        user.setId(id);
        user = userService.getUser(user);
        modelAndView.addObject("user", user);
        modelAndView.setViewName("userInfo");
        return modelAndView;
    }

    @RequestMapping(value = "/upd")
    public String upd(User user, HttpServletRequest request, MultipartFile imgFile) {
        String realPath = "E:\\code\\JavaIDEA\\自学\\Spring-MVC\\file-upload\\src\\main\\webapp\\static\\upload";
        if (!imgFile.isEmpty()) {
            String filename = imgFile.getOriginalFilename();
            String extension = FilenameUtils.getExtension(filename);
            if (extension.equalsIgnoreCase("jpg")
                    || extension.equalsIgnoreCase("jpeg")
                    || extension.equalsIgnoreCase("jpe")
                    || extension.equalsIgnoreCase("png")
                    || extension.equalsIgnoreCase("gif")
                    || extension.equalsIgnoreCase("bmp")
            ) {
                filename = UUIDutils.getUUID() + "." + extension;
                try {
                    File file = new File(realPath, request.getParameter("oldFile"));
                    file.delete();

                    imgFile.transferTo(new File(realPath, filename));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                user.setImgPath(filename);
                int count = userService.updUser(user);
                if (count > 0) {
                    return "redirect:/user/listPage";
                } else {
                    request.setAttribute("msg", "修改失败");
                }
            } else {
                request.setAttribute("msg", "图片类型不正确");
            }
        } else {
            request.setAttribute("msg", "没有选择图片");
        }
        return "userInfo";
    }
}
